//
//  LoginViewController.m
//  PlaneWar2
//
//  Created by HappyMilk 02 on 2017/4/7.
//  Copyright © 2017年 HappyMilk 02. All rights reserved.
//

#import "LoginViewController.h"
#import "PublicTool.h"
#import "LoginHeader.h"
#import "LeanCloudRegisterViewController.h"

@interface LoginViewController (){
    UIColor * m_mainColor;
    BOOL m_isOpenRegister;
}

@property (weak, nonatomic) IBOutlet UITextField *username_TF;
@property (weak, nonatomic) IBOutlet UITextField *password_TF;
@property (weak, nonatomic) IBOutlet UIButton *btn_login;


@end

@implementation LoginViewController

- (BOOL)shouldAutorotate{
    
    return NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [_btn_login setTitle:NSLocalizedStringFromTable(@"Log in", @"InfoPlist", nil) forState:UIControlStateNormal];

    
    ADD_OBSERVER(self, @selector(onAppInfoLoadedCB:), NOTIFICATION_APP_INFO_LOADED, nil);
    
    [self showWXLoginBtnIfNeed];
}

#pragma mark 初始化成功回调打开登陆
- (void)onAppInfoLoadedCB:(id)sender{
    [self showWXLoginBtnIfNeed];
}
- (void)showWXLoginBtnIfNeed{
    if([MPWeChat stander].m_needLogin){
        
        UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){__kwidth/2-40,self.btn_login.frame.origin.y+120,80,80}];
        [btn setImage:[UIImage imageNamed:@"wx_logo.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(wxLoginAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

- (void)wxLoginAction{
    QRCodeLoginViewController * qrCodeLoginViewController = [[QRCodeLoginViewController alloc]initWithNibName:@"QRCodeLoginViewController" bundle:nil];
    [self presentViewController:qrCodeLoginViewController animated:true completion:nil];
}

- (IBAction)dismissVC:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//普通登陆
- (IBAction)loginAction:(id)sender {
    
    NSString *username = _username_TF.text;
    NSString *password = _password_TF.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = LOC_STR(@"Please fill in the correct account or password! Account password must be less than 6!");
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    
    SHOW_HUD(LOC_STR(@"logging in..."));
    [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
        HIDE_HUD
        if (user != nil) {
            [MPWeChat stander].m_isLogin = true;
            [self dismissViewControllerAnimated:true completion:nil];
            
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"" message:LOC_STR(@"login successful") delegate:nil cancelButtonTitle:LOC_STR(@"Confirm") otherButtonTitles:nil, nil];
            [alertview show];
            
        } else {
            SHOW_HUD_MESSAGE(LOC_STR(@"Username or password is wrong!"));
        }
        
    }];
}
- (IBAction)registerAction:(id)sender {
    if ([MPWeChat stander].m_needLogin) {
        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器维护暂时停用注册功能！老用户可正常登录！新用户请使用第三方登录！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
    }
    else{
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"RegisterViewController"];
        [self presentViewController:vc animated:YES completion:nil];
    }
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.username_TF resignFirstResponder];
    [self.password_TF resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
